package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/11 18:38
 * @description
 */
public class Code21 {
    private List<Integer> list = new ArrayList<>();

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null && l2 == null) {
            return null;
        }
        setList(l1);
        setList(l2);
        Collections.sort(list);
        ListNode r = new ListNode(list.get(0));
        if (list.size() == 1) {
            return r;
        }
        getList(r, 1);
        return r;
    }

    private void setList(ListNode node) {
        if (node != null) {
            list.add(node.val);
            setList(node.next);
        }
    }

    private void getList(ListNode node, int level) {
        node.next = new ListNode(list.get(level));
        if (level != list.size() - 1) {
            getList(node.next, level += 1);
        }
    }
}
